package com.kbbywyclient.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.ShareFaceToFaceModule;
import com.kbbywyclient.presenter.ShareFaceToFacePresenter;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.ShareFaceToFaceContract;
import com.kbbywyclient.ui.helper.ShareHelper;
import com.kbbywyclient.ui.pop.SharePopWin;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.ImageLoaderUtils;
import com.kbbywyclient.util.QrCodeUtils;
import com.kbbywyclient.util.ViewUtils;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomToolBar;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/8/16 19:09.
 */

public class ShareFaceToFaceActivity extends BaseActivity implements ShareFaceToFaceContract.View{
    public static final String SHARE_URL_S ="url";
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_share_content)
    TextView tvShareContent;

    @Inject
    ShareFaceToFacePresenter presenter;

    private String shareUrl;
    private int qrCodeSize;

    private ShareHelper shareHelper;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_share_face_to_face;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        toolBar.setLeftViewOnClickLinster(v -> finish());
        //二维码宽高为屏幕宽度的1/2
        ViewGroup.LayoutParams params=ivQrCode.getLayoutParams();
        qrCodeSize=(int) (getResources().getDisplayMetrics().widthPixels/2.0f);
        params.width= qrCodeSize;
        params.height=qrCodeSize;
        ivQrCode.setLayoutParams(params);
    }

    @Override
    public void initData() {
        presenter.getShareDes();
        shareUrl=getIntent().getStringExtra(SHARE_URL_S);

        ApiServiceHelper.
                appointThreadNormal(Observable.from(new String[]{shareUrl}).map(s ->
                        //生成二维码
                        QrCodeUtils.generateQrCode(s,qrCodeSize,qrCodeSize)
                )).subscribe(new Subscriber<Bitmap>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                        ivQrCode.setImageBitmap(bitmap);
                        //二维码生成成功，设置用户头像
                        String avatar=PreUtils.getAvatar(getApplicationContext());
                        if(!TextUtils.isEmpty(avatar)){
                            ViewGroup.LayoutParams params=ivAvatar.getLayoutParams();
                            int avatarSize=(int) (qrCodeSize/5.0f);
                            params.width= avatarSize;
                            params.height=avatarSize;
                            ivAvatar.setLayoutParams(params);
                            ivAvatar.setVisibility(View.VISIBLE);
                            ImageLoaderUtils.displayImageDefaultRes(ivAvatar,API.PIC_ADDRESS+avatar,R.mipmap.icon_head_portrait);
                        }
                    }
                });

    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getShareFaceToFaceComponent(new ShareFaceToFaceModule(this)).inject(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("面对面分享");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(shareHelper!=null){
            shareHelper.onActivityResult(requestCode,resultCode,data);
        }
    }

    @OnClick({R.id.tv_wx,R.id.tv_wx_circle,R.id.tv_qq,R.id.tv_sina})
    void onClick(View view){
        ShareHelper.ShareBody shareBody =new ShareHelper.ShareBody();
        shareBody.url=shareUrl;
        Context context=ShareFaceToFaceActivity.this;
        if(PreUtils.isAuthenticated(context)){
            shareBody.title="您的好友"+
                    PreUtils.getTrueName(context)+
                    "邀请您申请信用卡,优惠好礼享不停";
        }else{
            shareBody.title="您的好友邀请您申请信用卡,优惠好礼享不停";
        }
        switch (view.getId()){
            case R.id.tv_wx:
                shareBody.platForm= SharePopWin.PLATFORM_WX;
                break;
            case R.id.tv_wx_circle:
                shareBody.platForm=SharePopWin.PLATFORM_WX_CIRCLE;
                break;
            case R.id.tv_qq:
                shareBody.platForm=SharePopWin.PLATFORM_QQ;
                break;
            case R.id.tv_sina:
                shareBody.platForm=SharePopWin.PLATFORM_SINA;
                break;
        }
        if(shareHelper==null){
            shareHelper=new ShareHelper(this,"面对面分享");
        }
        shareBody.statisticsTitle="面对面分享";
        shareHelper.share(shareBody);
    }

    @Override
    public void setShareDes(String shareDes) {
        ViewUtils.setText(tvShareContent,shareDes);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }
}
